//
// SDL.cpp for 203poisson in /home/faucon_l//Work/Tek2/maths/203poisson
//
// Made by leo faucon
// Login   <faucon_l@epitech.net>
//
// Started on  Thu Mar 24 18:00:30 2011 leo faucon
// Last update Sat Apr  9 16:45:01 2011 leo faucon
//

#include "SDL.hpp"

SDL_opt::SDL_opt(int y, int x, const std::string & titre)
{
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    std::cerr << "Erreur à l'initialisation de la SDL : " << SDL_GetError() << std::endl;
    exit(EXIT_FAILURE);
  }
  atexit(SDL_Quit);
  ecran = SDL_SetVideoMode(x, y, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  if (ecran == NULL) {
    std::cerr << "Impossible d'activer le mode graphique : " << SDL_GetError() << std::endl;
    exit(EXIT_FAILURE);
  }
  SDL_WM_SetCaption(titre.c_str(), NULL);
  TTF_Init();
  police = TTF_OpenFont("./arial.ttf", 16);
  policetexte = TTF_OpenFont("./arial.ttf", 20);

  background = NULL;
  texte = NULL;

  couleurJaune = {255, 255, 0, 0};
  couleurNoire = {0, 100, 200, 0};
  couleurRouge = {255, 0 , 0, 0};
  couleurVerte = {255, 165 , 0, 0};
}

SDL_opt::~SDL_opt()
{
  TTF_CloseFont(police);
  TTF_CloseFont(policetexte);
  TTF_Quit();
  SDL_FreeSurface(texte);
  SDL_FreeSurface(background);
  SDL_FreeSurface(ecran);
  SDL_Quit();
}

SDL_Surface	*SDL_opt::get_ecran() const
{
  return (ecran);
}

TTF_Font	*SDL_opt::get_police(int mode) const
{
  switch (mode)
    {
    case 0:
      return (police);
    case 1:
      return (policetexte);
    }
  return (NULL);
}

void		SDL_opt::aff_texte(const std::string & str, SDL_Color couleur)
{
  texte = TTF_RenderText_Blended(police, str.c_str(), couleur);
  SDL_BlitSurface(texte, NULL, ecran, &position);
  SDL_Flip(ecran);
}

void	SDL_opt::set_background(const std::string & path)
{
  position.x = 0;
  position.y = 0;
  background = SDL_LoadBMP(path.c_str());
  SDL_BlitSurface(background, NULL, ecran, &position);
  SDL_Flip(ecran);
}

void	SDL_opt::set_police(const std::string & path, int size, int mode)
{
  switch (mode)
    {
    case 0:
      police = TTF_OpenFont(path.c_str(), size);
      break;
    case 1:
      policetexte = TTF_OpenFont(path.c_str(), size);
      break;
    }
}
